//
//  TRSManager.h
//  TRS_SDK
//
//  Created by 824810056 on 2018/1/16.
//  Copyright © 2018年 牟松. All rights reserved.
//

#import <Foundation/Foundation.h>

#import "TRSDefaultCacheManager.h"
#import "TRSHTTPManager.h"
#import "TRSSystemEventConfig.h"
#import "TRSReachability2.h"
#import "TRSDBManager3.h"

#import "TRSCommen.h"
typedef void (^TRSSendDataSuccess)(void);
typedef void (^TRSSendDataFailure)(NSError *error);

typedef NS_ENUM(NSUInteger, TRSAppOpenType) {
    TRSAppOpenTypeLaunch,
    TRSAppOpenTypeForground,
    TRSAppOpenTypeActivedOnly,
};

@interface TRSManager : NSObject
{
    TRSDBManager3 *_DBManager3;
    TRSDefaultCacheManager *_defaultCacheManage;
    TRSHTTPManager *_HTTPManager;
}

@property (nonatomic, strong) TRSDefaultCacheManager *defaultCacheManage;
@property (nonatomic, strong) TRSDBManager3 *DBManager3;
@property (nonatomic, strong) TRSHTTPManager *HTTPManager;

@property (nonatomic, assign) UIBackgroundTaskIdentifier bgTask;

@property (nonatomic) TRSAppOpenType appOpenType;

@property (nonatomic, assign) NSInteger browsePageCount; // 本次会话访问页面次数

@property (nonatomic, strong) NSNumber *appStartTime; // app启动时间
@property (nonatomic, strong) NSNumber *appEndTime; // app结束时间

@property (nonatomic, strong) NSMutableArray *pageConfigArray; // 放页面数组
@property (nonatomic, strong) NSMutableArray *eventModelArray; // 放事件的数组

@property (nonatomic, assign) BOOL killAppTagSendPageEvent; // 用于当程序被杀死时，判断事件是否发出
@property (nonatomic, strong) NSArray *killPageEventArray; // 用于程序被杀死，Background通知中用
@property (nonatomic, assign) BOOL killAppTagSendPageEvent2;
@property (nonatomic, strong) NSArray *killPageEventArray2;

@property (nonatomic, assign) BOOL logEnable;
@property (nonatomic, assign) BOOL debugEnable;
@property (nonatomic, assign) BOOL debugSuccess; // 发送debug模式是否成功

@property (nonatomic, assign) BOOL hasDeviceID; // 用于判断是否有deviceID
@property (nonatomic, assign) BOOL hasSendDeviceID; // 判断deviceID是否发送成功

@property (nonatomic, assign) BOOL hasUID; // 用于判断是否有用户信息
@property (nonatomic, assign) BOOL hasSendUID; // 判断用户信息是否发送成功


+ (TRSManager *)sharedManager;

- (void)browsePageCountAdd; // 页面浏览次数自增一
- (void)launchCountAdd; // 启动次数自增一

- (void)sendPageEventWithDataArray:(NSArray *)dataArray; // 供页面结束时发送数据调用

- (void)sendDebugPageEventWithDataArray:(NSArray *)dataArray; // debug模式下发送数据

- (void)sendDebugStateDeviceMessage; // debug下发送设备信息

- (void)sendUserInfo;

@end

// 去掉临时表后的发送逻辑
/*
 - (void)sendPageEventData2{
 NSInteger count = [self->_DBManager managerGatDataTotalCountWithDataType:TRSDataTypePageEvent];
 if (count == 0) {
 return;
 } else if (count > 0 && count <= 50) {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypePageEvent count:0];
 
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条PageEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除数据失败*****");
 }
 
 return;
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"发送数据失败");
 if (error.code == -2) {
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"%ld条PageEvent数据有问题，清空该表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****PageEvent数据有问题，清空该表失败*****");
 }
 }
 return;
 }];
 
 } else {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypePageEvent count:50];
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条PageEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除数据失败*****");
 }
 // 再次发送数据请求
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 [self sendPageEventData2];
 });
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"发送数据失败");
 if (error.code == -2) {
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"%ld条PageEvent数据有问题，清空该表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****PageEvent数据有问题，清空该表失败*****");
 }
 }
 return;
 }];
 
 }
 }
 - (void)sendSystemEventData2{
 NSInteger count = [self->_DBManager managerGatDataTotalCountWithDataType:TRSDataTypeSystemEvent];
 if (count == 0) {
 return;
 } else if (count > 0 && count <= 50) {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypeSystemEvent count:0];
 
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条SystemEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypeSystemEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条SystemEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送SystemEvent数据成功，删除数据失败*****");
 }
 return;
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"发送数据失败");
 if (error.code == -2) {
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypeSystemEvent dataModelArray:dataArray]) {
 TRSNSLog(@"%ld条SystemEvent数据有问题，清空该表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****SystemEvent数据有问题，清空该表失败*****");
 }
 }
 return;
 }];
 
 } else {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypeSystemEvent count:50];
 
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 TRSNSLog(@"成功发送%ld条SystemEvent数据", (unsigned long)dataArray.count);
 // 再次发送数据请求
 @strongify(self);
 if (!self) return;
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypeSystemEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条SystemEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送SystemEvent数据成功，删除数据失败*****");
 }
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 [self sendSystemEventData2];
 });
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"发送数据失败");
 if (error.code == -2) {
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypeSystemEvent dataModelArray:dataArray]) {
 TRSNSLog(@"%ld条SystemEvent数据有问题，清空该表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****SystemEvent数据有问题，清空该表失败*****");
 }
 }
 return;
 }];
 
 }
 }
 */

/**
 发送数据
 发送逻辑 》》
 if (临时表有数据) {
 发送临时表数据
 
 发送成功：
 if(正式表有数据){
 if(count == 0){
 return;
 } else if (count > 0 && count <= 100){
 发送正式表数据
 发送成功：   return
 发送时失败： 将数据写入临时表 return
 } else {
 发送正式表数据
 发送成功：   [self sendData]   递归发送，直到发送完所有数据，或符合别的情况才会停下
 发送时失败： 将数据写入临时表 return
 }
 }
 
 发送失败：
 将临时表数据再写入临时表 return
 } else {
 发送正式数据
 if(正式表有数据){
 if(count == 0){
 return;
 } else if (count > 0 && count <= 100){
 发送正式表数据
 发送成功：   return
 发送时失败： 将数据写入临时表 return
 } else {
 发送正式表数据
 发送成功：   [self sendData]   递归发送，直到发送完所有数据，或符合别的情况才会停下
 发送时失败： 将数据写入临时表 return
 }
 }
 }
 */

/*
 - (void)sendPageEventData{
 NSInteger tempCount = [_DBManager managerGatDataTotalCountWithDataType:TRSDataTypePageEventTemp];
 if (tempCount != 0) { // 临时表有数据时，先发送临时表再发送其他数据
 NSArray *tempDataArray = [_DBManager managerGetDataWithDataType:TRSDataTypePageEventTemp count:0];
 @weakify(self);
 [self sendDataWithWithDataArray:tempDataArray Success:^{
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)tempDataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEventTemp dataModelArray:nil]) {
 TRSNSLog(@"成功删除Temp表%ld条PageEvent数据", (unsigned long)tempDataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除Temp表数据失败*****");
 }
 NSInteger count = [self->_DBManager managerGatDataTotalCountWithDataType:TRSDataTypePageEvent];
 if (count == 0) {
 return;
 } else if (count > 0 && count <= 50) {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypePageEvent count:0];
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条PageEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除数据失败*****");
 }
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent失败，成功从原表删除%ld条PageEvent数据，将PageEvent数据转入temp表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent失败，删除原表数据失败*****");
 return; // 防止出现重复数据
 }
 if ([self->_DBManager managerInsertDataWithDataType:TRSDataTypePageEventTemp dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent数据失败，插入临时表%ld条数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送失败插入临时表%ld条PageEvent数据失败，数据丢失******", (unsigned long)dataArray.count);
 }
 return;
 }];
 
 
 } else {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypePageEvent count:50];
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条PageEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除数据失败*****");
 
 }
 
 // 再次发送数据请求
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 [self sendPageEventData];
 });
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent失败，成功从原表删除%ld条PageEvent数据，将PageEvent数据转入temp表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent失败，删除原表数据失败*****");
 return; // 防止出现重复数据
 }
 if ([self->_DBManager managerInsertDataWithDataType:TRSDataTypePageEventTemp dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent数据失败，插入临时表%ld条数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送失败插入临时表%ld条PageEvent数据失败，数据丢失******", (unsigned long)dataArray.count);
 }
 return;
 }];
 
 }
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"提交temp数据失败");
 if (error.code == -2) {
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEventTemp dataModelArray:nil]) {
 TRSNSLog(@"Temp表%ld条PageEvent数据有问题，清空该表", (unsigned long)tempDataArray.count);
 } else {
 TRSNSLog(@"*****Temp表PageEvent数据有问题，清空该表*****");
 }
 }
 return;
 }];
 } else { // 临时表没有数据的时候直接发送数据
 
 NSInteger count = [self->_DBManager managerGatDataTotalCountWithDataType:TRSDataTypePageEvent];
 if (count == 0) {
 return;
 } else if (count > 0 && count <= 50) {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypePageEvent count:0];
 
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条PageEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除数据失败*****");
 }
 
 return;
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent失败，成功从原表删除%ld条PageEvent数据，将PageEvent数据转入temp表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent失败，删除原表数据失败*****");
 return; // 防止出现重复数据
 }
 if ([self->_DBManager managerInsertDataWithDataType:TRSDataTypePageEventTemp dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent数据失败，插入临时表%ld条数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送失败插入临时表%ld条PageEvent数据失败，数据丢失******", (unsigned long)dataArray.count);
 }
 return;
 }];
 
 } else {
 NSArray *dataArray = [self->_DBManager managerGetDataWithDataType:TRSDataTypePageEvent count:50];
 @weakify(self);
 [self sendDataWithWithDataArray:dataArray Success:^{
 
 @strongify(self);
 if (!self) return;
 TRSNSLog(@"成功发送%ld条PageEvent数据", (unsigned long)dataArray.count);
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"成功删除%ld条PageEvent数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent数据成功，删除数据失败*****");
 }
 // 再次发送数据请求
 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
 [self sendPageEventData];
 });
 } failure:^(NSError *error){
 @strongify(self);
 if (!self) return;
 if ([self->_DBManager managerDeleteDataWithDataType:TRSDataTypePageEvent dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent失败，成功从原表删除%ld条PageEvent数据，将PageEvent数据转入temp表", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送PageEvent失败，删除原表数据失败*****");
 return; // 防止出现重复数据
 }
 if ([self->_DBManager managerInsertDataWithDataType:TRSDataTypePageEventTemp dataModelArray:dataArray]) {
 TRSNSLog(@"发送PageEvent数据失败，插入临时表%ld条数据", (unsigned long)dataArray.count);
 } else {
 TRSNSLog(@"*****发送失败插入临时表%ld条PageEvent数据失败，数据丢失******", (unsigned long)dataArray.count);
 }
 return;
 }];
 
 }
 
 }
 }
*/
